package com.woniuxy.project115.kaer.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project115.kaer.mall.entity.ProductType;
import com.woniuxy.project115.kaer.mall.mapper.ProductTypeMapper;
import com.woniuxy.project115.kaer.mall.service.ProductTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.project115.kaer.mall.util.RedisKey;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyc
 * @since 2025年02月17日
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ProductTypeMapper productTypeMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private  RedisTemplate<String, Object> redisObjectTemplate;

    @Override
    public void addProductType(String productTypeName, Integer parentId) throws Exception {
        //删除缓存中所有的商品类别,保持数据的一致性
        stringRedisTemplate.delete(RedisKey.CACHE_TYPE_TREE());
        //判断商品类别名是否重复(0表示可用,1表示禁用,2表示删除)
        ProductType productType = productTypeMapper.selectOne(new QueryWrapper<ProductType>().eq("type_name", productTypeName).ne("type_state", 2));
        if (productType != null) {
            throw new Exception("商品类别名重复");
        }
        ProductType addProductType = new ProductType();
        addProductType.setTypeName(productTypeName)
                      .setParentId(parentId);
        if (parentId==0){
            addProductType.setTypeState(0);
        }else {
            ProductType parentType = productTypeMapper.selectById(new QueryWrapper<ProductType>().eq("type_id", parentId));
            if (parentType == null) {
                throw new Exception("父级商品类别不存在");
            }
            if (parentType.getTypeState()==1){
                throw new Exception("父级商品类别已禁用");
            }
        }
        productTypeMapper.insert(addProductType);
        //新增商品类别后,删除缓存中所有的商品类别,保持数据的一致性(延迟双删)
        rabbitTemplate.convertAndSend("","waiting_delete_cache_queue",RedisKey.CACHE_TYPE_TREE());
    }

    @Override
    public void deleteProductType(Integer typeId) {
        ProductType productType = productTypeMapper.selectById(typeId);
        if (productType == null) {
            throw new RuntimeException("商品类别不存在");
        }
        if (productType.getTypeState()==2){
            throw new RuntimeException("商品类别已删除");
        }
        productType.setTypeState(2);
        productTypeMapper.updateById(productType);
        delChildType(typeId);
    }

    private void delChildType(Integer typeId) {
        List<ProductType> currentTypeList = productTypeMapper.selectList(new QueryWrapper<ProductType>().eq("parent_id", typeId));
        for (ProductType childType : currentTypeList) {
            childType.setTypeState(2);
            productTypeMapper.updateById(childType);
            deleteProductType(childType.getTypeId());
        }
    }

    @Override
    public void disableProductType(Integer typeId) {
        ProductType productType = productTypeMapper.selectById(typeId);
        if (productType == null) {
            throw new RuntimeException("商品类别不存在");
        }
        if (productType.getTypeState()==2){
            throw new RuntimeException("商品类别已被删除");
        }
        //禁用当前商品类别,同时禁用所有子商品类别
        if (productType.getTypeState()==0){
            productType.setTypeState(1);
            disableChildType(typeId);
        } else if (productType.getTypeState()==1) {
            productType.setTypeState(0);
            enableChildType(typeId);
        }
        productTypeMapper.updateById(productType);
    }

    private void enableChildType(Integer typeId) {
        //启用当前商品类别,同时启用所有父商品类别
        ProductType productType = productTypeMapper.selectById(typeId);
        ProductType parentType = productTypeMapper.selectOne(new QueryWrapper<ProductType>().eq("type_id", productType.getParentId()));
        if (parentType!=null && parentType.getTypeState()==1){
            parentType.setTypeState(0);
            productTypeMapper.updateById(parentType);
            enableChildType(parentType.getTypeId());
        }
    }

    private void disableChildType(Integer typeId) {
        List<ProductType> currentTypeList = productTypeMapper.selectList(new QueryWrapper<ProductType>().eq("parent_id", typeId));
        for (ProductType childType : currentTypeList) {
            //禁用当前商品类别,同时禁用所有子商品类别
            if (childType.getTypeState()==0){
                childType.setTypeState(1);
            }
            productTypeMapper.updateById(childType);
            disableChildType(childType.getTypeId());
        }
    }
}
